using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using UmlDesigner.Model;

namespace UmlDesigner
{
    public partial class LinkPropertiesForm : Form
    {
        LinkWidget link = null;
        LinkWidget targetLink = null;

        internal LinkPropertiesForm(LinkWidget l)
        {
            targetLink = l;
            link = new LinkWidget(l.GetSource(), l.GetDestination());
            targetLink.ShallowCopyLinkWidget(link);

            InitializeComponent();
            Icon = Properties.Resources.ApplicationIcon;

            SetupControlsToMatchLink();
            UpdateControlsVisibility();
            GenerateSampleCode();
        }

        private void GenerateSampleCode()
        {
            if (AssociationSelected())
            {
                StringWriter exampleCode = new StringWriter();
                CodeGenerationSettings settings = new CodeGenerationSettings("", "GeneratedCode", CodeGenerationSettings.GenerationSource.SelectionOnly, false, false, false);
                settings.StartNewItem(exampleCode, 0);
                link.GenerateCode(settings);
                exampleCodeRichTextBox.Text = exampleCode.ToString();
            }
            else
            {
                exampleCodeRichTextBox.Text = "-- NA --";
            }
        }

        private void SetupControlsToMatchLink()
        {
            if (link.LinkType == LinkWidget.LineType.NoteLink)
            {
                linkTypeComboBox.Items.Add("Note Link");
                linkTypeComboBox.Enabled = false;
            }
            else
            {
                linkTypeComboBox.Items.Add("Association");
                linkTypeComboBox.Items.Add("Inheritence");
                if (link.LinkType == LinkWidget.LineType.Association)
                {
                    linkTypeComboBox.SelectedIndex = 0;

                    nameTextBox.Text = link.Name;

                }
                else
                {
                    linkTypeComboBox.SelectedIndex = 1;
                }
            }
            accessComboBox.SelectedItem = link.AccessType;
            multiplicityComboBox.SelectedItem = link.MultiplicityType;
            publicPropertyCheckBox.Checked = link.GenerateProperty;
        }

        private void okButton_Click(object sender, EventArgs e)
        {
            if (linkTypeComboBox.Enabled)
            {
                if (linkTypeComboBox.SelectedIndex == 0)
                {
                    link.LinkType = LinkWidget.LineType.Association;
                }
                else
                {
                    link.LinkType = LinkWidget.LineType.Inheritance;
                }
            }

            link.ShallowCopyLinkWidget(targetLink);
        }

        private void linkTypeComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            UpdateControlsVisibility();
            GenerateSampleCode();
        }

        private void UpdateControlsVisibility()
        {
            if (AssociationSelected())
            {
                // If its association, show all the association settings.
                associationGroupBox.Show();
            }
            else
            {
                // Otherwise its not association, so hide any association related stuff.
                associationGroupBox.Hide();
            }
        }

        private bool AssociationSelected()
        {
            return linkTypeComboBox.SelectedIndex == 0;
        }

        private void nameTextBox_TextChanged(object sender, EventArgs e)
        {
            if (nameTextBox.Text.Trim().Length > 0)
            {
                link.Name = nameTextBox.Text.Trim();
                GenerateSampleCode();
            }
            else
            {
                exampleCodeRichTextBox.Text = "-- Not Available --";
            }
        }

        private void accessComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            link.AccessType = accessComboBox.Text;
            GenerateSampleCode();
        }

        private void multiplicityComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            link.MultiplicityType = multiplicityComboBox.Text;
            GenerateSampleCode();
        }

        private void publicPropertyCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            link.GenerateProperty = publicPropertyCheckBox.Checked;
            GenerateSampleCode();
        }

        private void LinkPropertiesForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            link.DeallocateSlots();
        }
    }
}